﻿#region  Copyright (C) 2013 Unisys Corporation

// =============================================================================================
// All Rights Reserved. This work contains trade secrets and confidential material of UNISYS, 
// and its use or disclosure in whole or in part without explicit written permission of 
// UNISYS is prohibited. 
//  
// Product Name          :  Unisys e-Workflow and Imaging 8.2
// File Name             :  Serializer.cs
// Authored By           :  Unisys Corporation
// =============================================================================================
//	Modified by                         Date            Version
// =============================================================================================
//	ronald.aguilar@cr.unisys.com      Jun 06, 2013    Initial Version
// =============================================================================================

#endregion

using System.IO;
using System.Xml.Serialization;

namespace Unisys.DuplicateCheck.Entity.Helper
{

    /// <summary>
    /// XML Serializing class.
    /// </summary>
    public static class Serializer
    {
        /// <summary>
        /// Write the serializable object to a file in CREATE mode, NO APPEND, overwritte the file if it already exists.
        /// </summary>
        /// <typeparam name="T">Type of the object to be serialized.</typeparam>
        /// <param name="objSerilizable">Serializable object.</param>
        /// <param name="filePath">Complete path of the file that would be written.</param>
        public static void Serialize<T>(T objSerilizable, string filePath) where T : class, new()
        {
            Stream stream = File.Open(filePath, FileMode.Create);
            var serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stream, objSerilizable);
            stream.Close();
        }

        /// <summary>
        /// Write the serializable object to a file in CREATE mode, NO APPEND, overwritte the file if it already exists.
        /// </summary>
        /// <typeparam name="T">Type of the object to be serialized.</typeparam>
        /// <param name="objSerilizable">Serializable object.</param>
        /// <param name="filePath">Complete path of the file that would be written.</param>
        public static void XmlSerializeToFile<T>(T objSerilizable, string filePath) where T : class, new()
        {
            //create an empty namespace
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            using (Stream stream = File.Open(filePath, FileMode.Create))
            {
                var serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(stream, objSerilizable, ns);
                stream.Close();
            }
        }
    }
}
